package com.baosight.sczx.zm.cg.service;

import java.util.HashMap;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.threadlocal.UserSession;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.cs.WorkSchedule;
import com.baosight.sczx.zm.cg.domain.ZMCGChangeWork;
import com.baosight.sczx.zm.cg.domain.ZMCGChangeWorkDetail;
import com.baosight.sczx.zm.cg.domain.ZMCGDutyscheduleDeploy;
import com.baosight.sczx.zm.cg.domain.ZMCGRecord;

/**
 * 排班
 * <p>Title: ServiceZMCG71.java</p> 
 */
public class ServiceZMCG71 extends ServiceEPBase {

	/**
	 * 初始化方法 
	 */
	@Override
	public EiInfo initLoad(EiInfo inInfo) {
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
		inInfo.addBlock(WorkSchedule.getWorkScheduleBlock("cmes.cp.workNum", false));		 
		EiInfo outInfo = this.query(inInfo);
		return outInfo;
	}

	/**
	 * 查询方法 
	 */
	@Override
	public EiInfo query(EiInfo inInfo) { 
		if(null != inInfo.getBlock("inqu_status")){
			String fdVirtualDate = inInfo.getBlock("inqu_status").getCellStr(0, "fdVirtualDate");
			String fdImportRecord = inInfo.getBlock("inqu_status").getCellStr(0, "fdImportRecord");
			if(null != fdVirtualDate && !("").equals(fdVirtualDate)){
				StaticTools.setQueryParameters(inInfo, "fdVirtualDate", fdVirtualDate, true);//添加查询条件 
			}
			if(null != fdImportRecord && !("").equals(fdImportRecord)){
				StaticTools.setQueryParameters(inInfo, "fdImportRecord", fdImportRecord, true);//添加查询条件 
			}
			}
		String fdMiningId = (String) UserSession.getInSessionProperty("miningCode");//矿ID的默认值
		StaticTools.setQueryParameters(inInfo, "fdMiningId", UserSession.getInSessionProperty("miningCode"), true);
		EiInfo outInfo = super.query(inInfo, "ZMCGRecord.query", new ZMCGRecord());
		outInfo.addBlock(WorkSchedule.getWorkScheduleBlock("cmes.cp.workNum", false));	
		outInfo.set("fdMiningId", UserSession.getInSessionProperty("miningCode"));//矿ID的默认值
		return outInfo;
	}


/**
 * <p>Function: insert</p>
 * <p>Description: 新增</p>
 * <p>return value: ServiceZMCP02</p>
 * <p>throws Exception:</p>
 * <p>History:
 *    2016-03-21下午03:07:57 张坤
 * </p>
 *
 */
@Override	
public EiInfo insert(EiInfo inInfo) {
	EiInfo out=new EiInfo(); 		
	String date = StaticTools.getDate();
	String fdLeaderName = (String) UserSession.getInSessionProperty("UserCname");
	ZMCGRecord _dom = new ZMCGRecord();
	String fdMiningId = (String) UserSession.getInSessionProperty("miningCode");//矿ID的默认值
	String fdMiningName = (String) UserSession.getInSessionProperty("miningName");//矿名称的默认值
	EiBlock eiBlock = inInfo.getBlock("result");
	eiBlock.setBlockMeta(_dom.eiMetadata);
	int rowcount = eiBlock.getRowCount();
	int i = 0;
	for (; i < rowcount; i++) {
		_dom.fromMap(eiBlock.getRow(i));
		_dom.setFdCurrentDate(date);
		_dom.setFdLeaderName(fdLeaderName);
		_dom.setFdVirtualDate(WorkSchedule.getVirtualDate(_dom.getFdCurrentDate(),_dom.getFdWorknumId()));
		_dom.setFdMiningId(fdMiningId);
		_dom.setFdMiningName(fdMiningName);
		_dom.setFdKeyId(TablePk.getInstance().getNextValue("T_CG_RECORD"));
		this.dao.insert("ZMCGRecord.insert",_dom); 
	}
	out = this.query(inInfo);
	out.setStatus(1);
	out.setMsg("操作" + rowcount + "记录成功");
	return out;  
}

/**
 * <p>Function: update</p>
 * <p>Description: 修改</p>
 * <p>return value: ServiceZMCP02</p>
 * <p>throws Exception:</p>
 * <p>History:
 *    2016-03-21下午04:20:50 张坤
 * </p>
 *
 */
@Override
public EiInfo update(EiInfo inInfo) {
	//校验是否重复
	EiInfo out=new EiInfo();
	String date = StaticTools.getDate();
	String fdMiningId = (String) UserSession.getInSessionProperty("miningCode");//矿ID的默认值
	String fdMiningName = (String) UserSession.getInSessionProperty("miningName");//矿名称的默认值
	String fdLeaderName = (String) UserSession.getInSessionProperty("UserCname");
	ZMCGRecord _dom = new ZMCGRecord();
	EiBlock eiBlock = inInfo.getBlock("result");
	eiBlock.setBlockMeta(_dom.eiMetadata);
	int rowcount = eiBlock.getRowCount();
	int i = 0;
	for (; i < rowcount; i++) {
		_dom.fromMap(eiBlock.getRow(i));
		_dom.setFdCurrentDate(date);
		_dom.setFdLeaderName(fdLeaderName);
		_dom.setFdVirtualDate(WorkSchedule.getVirtualDate(_dom.getFdCurrentDate(),_dom.getFdWorknumId()));
		_dom.setFdMiningId(fdMiningId);
		_dom.setFdMiningName(fdMiningName);
		this.dao.update("ZMCGRecord.update",_dom);
		
	}
	out = this.query(inInfo);
	out.setStatus(1);
	out.setMsg("操作" + rowcount + "记录成功");
	return out;  
	
}	

/**
 * <p>Function: delete</p>
 * <p>Description: 删除</p>
 * <p>return value: ServiceZMCP02</p>
 * <p>throws Exception:</p>
 * <p>History:
 *    2016-03-21下午04:30:17 张坤
 * </p>
 *
 */
@Override
public EiInfo delete(EiInfo inInfo) { 
	EiInfo out=new EiInfo(); 
	ZMCGRecord _dom = new ZMCGRecord();
	EiBlock eiBlock = inInfo.getBlock("result");
	eiBlock.setBlockMeta(_dom.eiMetadata);
	int rowcount = eiBlock.getRowCount();
	int i = 0;
	for (; i < rowcount; i++) {
		_dom.fromMap(eiBlock.getRow(i));
		this.dao.delete("ZMCGRecord.delete", _dom);
	}
	EiInfo outInfo = this.query(inInfo);
	out.setStatus(1);
	out.setMsg("操作" + rowcount + "记录成功");
	return outInfo;

}	

}

